﻿using HuiZe.education.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HuiZe.education.View
{
    /// <summary>
    /// PART_ResourceCenter.xaml 的交互逻辑
    /// </summary>
    public partial class PART_ResourceCenter : UserControl
    {
        public Action<bool> CloseCallback { get; set; }

        public Func<int, bool> CollectCallback { get; set; }

        public PART_ResourceCenter()
        {
            InitializeComponent();
            PART_IsCollected.Checked += PART_IsCollected_Checked;
            PART_IsCollected.Unchecked += PART_IsCollected_Unchecked;
        }

        public void Load(respResourceChildModel _data)
        {
            _is_loading = false;
            PART_Host.Reset();
            _model_src = _data;
            PART_Host.Load(_data);
            if (_data == null)
            {
                this.Visibility = Visibility.Collapsed;
                return;
            }

            _is_loading = true;
            _cache_collection = _data.kind_item.collection == 1;
            PART_Title.Text = _data.kind_item.kind_item_name;
            PART_Count_View.Text = _data.kind_item.shownum.ToString();
            PART_Count_Collected.Text = _data.kind_item.collectionnum.ToString();
            PART_IsCollected.IsChecked = new bool?(_data.kind_item.collection == 1);

            this.Visibility = Visibility.Visible;
            _is_loading = false;
        }

        void PART_IsCollected_Unchecked(object sender, RoutedEventArgs e)
        {
            if (_is_loading) return;

            if (CollectCallback != null && _model_src != null && _model_src.kind_item != null)
            {
                if (CollectCallback(_model_src.kind_item.id))
                {
                    _model_src.kind_item.collectionnum--;
                    PART_Count_Collected.Text = _model_src.kind_item.collectionnum.ToString();
                }
            }
        }

        void PART_IsCollected_Checked(object sender, RoutedEventArgs e)
        {
            if (_is_loading) return;

            if (CollectCallback != null && _model_src != null && _model_src.kind_item != null)
            {
                if (CollectCallback(_model_src.kind_item.id))
                {
                    _model_src.kind_item.collectionnum++;
                    PART_Count_Collected.Text = _model_src.kind_item.collectionnum.ToString();
                }
            }
        }

        void PART_Close_Click(object sender, RoutedEventArgs e)
        {
            PART_Host.Reset();
            this.Visibility = Visibility.Collapsed;
            if (CloseCallback != null)
                CloseCallback(_cache_collection != PART_IsCollected.IsChecked.Value);
        }


        bool _is_loading;
        bool _cache_collection;
        respResourceChildModel _model_src;
    }
}
